<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <input id="id" name="id" type="hidden" value="-1">
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">套餐名</label>
            <div class="layui-input-block">
                <input id="name" type="text" name="name" lay-verify="required" lay-reqtext="套餐名不能为空" placeholder="请输入套餐名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">套餐类型</label>
            <div class="layui-input-block">
                <input type="radio" name="packagetype" value="话费" title="话费" checked="">
                <input type="radio" name="packagetype" value="流量" title="流量">
                <input type="radio" name="packagetype" value="宽带" title="宽带">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">价格</label>
            <div class="layui-input-block">
                <input id="price" type="number" name="price" lay-verify="required" lay-reqtext="价格" placeholder="请输入价格" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">大小</label>
            <div class="layui-input-block">
                <input id="count" type="text" name="count" placeholder="请输入数量" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script src="../../js/common.js"></script>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        let isEdit = false
        let mobilepackage = JSON.parse(localStorage.getItem('mobilepackage'))
        if (mobilepackage) {
            isEdit = true
            $('#id').val(mobilepackage.id)
            $('#name').val(mobilepackage.name)
            $('#price').val(mobilepackage.price)
            $('#count').val(mobilepackage.count)
        }
        localStorage.removeItem('mobilepackage')
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if (isEdit) {
                $.ajax({
                    url: '../mobile-package/update',
                    method: "POST",
                    dataType: 'json',
                    data: data.field,
                    success: function (resp) {
                        var index = layer.alert('保存成功', function () {
                            // 关闭弹出层
                            layer.close(index);
                            layer.close(parentIndex);
                            window.location.reload();
                        });
                    }
                })
            } else {
                $.ajax({
                    url: '../mobile-package/create',
                    method: "POST",
                    dataType: 'json',
                    data: data.field,
                    success: function (resp) {
                        var index = layer.alert('保存成功', function () {
                            // 关闭弹出层
                            layer.close(index);
                            layer.close(parentIndex);
                            window.location.reload();
                        });
                    }
                })
            }
            return false;
        });

    });
</script>